package com.zxx.otherchaos.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

/**
 * @author zxx on 2020/12/29
 */
public class ToastSimpleUtils {

    private ToastSimpleUtils() {
        /* cannot be instantiated 防止被实例化*/
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    private static Toast mToast;
    //private static Handler mHandler = new Handler();
    private static Handler mHandler = new Handler(Looper.getMainLooper());
    private static Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mToast.cancel();
        }
    };

    /**
     * 显示吐司
     */
    public static void showToast(Context context, String text, int duration) {
        mHandler.removeCallbacks(runnable);
        if (mToast != null) {
            mToast.setText(text);   // 当前token正在显示，直接修改显示的文本
            mToast.setDuration(duration);
        } else {
            /*这样的话，不管传递什么context进来，都只会引用全局唯一的context，不会产生内存泄露*/
            mToast = Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT);
            //mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
//            mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        mHandler.postDelayed(runnable, duration);
        mToast.show();
    }

    // 默认duration为1000
    public static void ToastMsg(Context mContext, String text) {
        ToastMsg(mContext, text, 1000);
    }


    public static void ToastMsg(Context context, String str, int duration) {
        showToast(context, str, duration);
    }


    //SnackBar---导入design包
    public static void showShortSnackBar(View view, String message) {
        Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
    }

    public static void showLongSnackBar(View view, String message) {
        Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
    }



    public void ToastShow(Context context,View view) {
        Toast toast = new Toast(context.getApplicationContext());
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, -200);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }


}
